﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Drawing;
using System.IO;

namespace TrafficControl
{
    class Road
    {
        //TO-DO
        // Define a field of collection type, to restore cars
        // After a little period of time (maybe 100ms), calculate all cars' situation
        // Don't forget "transfer" cars from one road to another one if it go to another road


        const int LEFT = -1;
        const int AHEAD = -1;
        const int RIGHT = -1;

        public int x = 0;
        public int y = 100;
        public float rotation = 0;
        public Random random = new Random();
        public string path;
        public Image newImage;

        Queue<Car> carQueue = new Queue<Car>();
        //Car car = new Car();

        public TrafficLight[] lights = new TrafficLight[2];
        // Create point for upper-left corner of image.
        public Point ulCorner = new Point(0, 100);

        public Road() { 
            //path = @"/cars/" + random.Next(0,2) + ".png";
            //newImage = Image.FromFile(Directory.GetCurrentDirectory().Substring(0, Directory.GetCurrentDirectory().Length - 10) +path);
            for (int i = 0; i < 2; i++) {
                this.lights[i] = new TrafficLight();
            }
        }

        public void Run()
        {


            while (true) {

                this.generateCars();

                this.calculateCarsPosition();

            }
            //car.goStraight();
            //while (x < 100)
            //{
            //    x++;
            //    ulCorner.X = x;
            //    Console.WriteLine("now the position is " + x);
            //    Thread.Sleep(10);
            //}
            //rotation = 4;
            //while (y < 600)
            //{
            //    y++;
            //    ulCorner.Y = y;
            //    rotation++;
            //    Console.WriteLine("now the position is " + x);
            //    Thread.Sleep(100);
            //    //rotation = 0;
            //}
        }

        public void generateCars() {

            Car car = new Car();
            this.carQueue.Enqueue(car);
            
        }

        public void calculateCarsPosition() {
            foreach (Car car in this.carQueue)
            {
                if (isSafeDistance(car, getCarInFront(car)))
                {
                    if (car.getCarDirection() == LEFT && this.lights[0].isTrafficLightGreen()) {
                        if (car.isCarInCrossSection())
                        {
                            car.turnLeft();
                        }
                        else
                        {
                            car.goStraight();
                        }
                    }else if (car.getCarDirection() == AHEAD && this.lights[0].isTrafficLightGreen()) {
                            car.goStraight();

                    }
                    else if (car.getCarDirection() == RIGHT && this.lights[1].isTrafficLightGreen())
                    {
                        if (car.isCarInCrossSection())
                        {
                            car.turnRight();
                        }
                        else
                        {
                            car.goStraight();
                        }
                    }
                    else {
                        car.wait();
                    }


                }
                else {
                    car.wait();
                }
            }
        }

        public Car getCarInFront(Car car) {
            return null;
        }

        public Boolean isSafeDistance(Car carA, Car carB) {
            return true;
        }
    }
}
